package com.sig.util;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

/**
 * Druid连接池工具
 *
 * @author sig
 * @since 2019/1/26
 */
public final class DruidUtil {
    private static DataSource source;

    private DruidUtil() {
    }

    public static void init() {
        final Properties props = new Properties();
        try {
            props.load(new FileInputStream("config/druid.properties"));
            source = DruidDataSourceFactory.createDataSource(props);
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage(), e);
        }
    }

    public static Connection getConnection() throws SQLException {
        if (source == null) init();
        return source.getConnection();
    }
}
